//table头信息 不变
var sm_columnData = [
    {"Width": "7%", "ColumnName": "ID"},
    {"Width": "7%", "ColumnName": "短信批次"},
    {"Width": "8%", "ColumnName": "客户名称"},
    {"Width": "7%", "ColumnName": "责任人"},
    {"Width": "7%", "ColumnName": "客户类型"},
    {"Width": "7%", "ColumnName": "客户等级"},
    {"Width": "7%", "ColumnName": "联系人"},
    {"Width": "7%", "ColumnName": "手机号"},
    {"Width": "8%", "ColumnName": "身份证号"},
    {"Width": "7%", "ColumnName": "来源"},
    {"Width": "7%", "ColumnName": "所属行业"},
    {"Width": "7%", "ColumnName": "录入时间"},
    {"Width": "7%", "ColumnName": "状态"},
    {"Width": "7%", "ColumnName": "重新提交"}
];

//操作按钮 不变
var sm_operateHtml = "<div class='form-inline'>" +
    " <button type='button' class='btn btn-primary innerButtons'  value='edit'>编辑</button>" +
    "</div>";


//用户管理分页函数
var gotoPage = function (index) {
    var url = webPath + "/crm/1";
    //TODO FORm val()
    //var postData =  {"pageSize": pageSize, "pageNumber": index,
    //    "customerName":customerName,"recordTime":recordTime,"nextContectTime":nextContectTime};
    var postData = $("#m-customerForm").serialize();
    var options = {"passArguments": index}
    postAjax(url, postData, gotoPageSuccess, options);
};

var gotoPageSuccess = function (result, passArguments) {
    //TODO remove
    var result = '{"totalNumber":3,"list":[{"customerId":"22", "customerName":"张三", "customerGrade":"潜在客户",' +
        '"idNumber":"333333333333333333","recordTime":"2015-6-9","contact":"李四",' +
        '"responsiblePerson":"责任人","customerType":"生产企业","source":"网站注册","phoneNumber":"111111111",' +
        '"industry":"钢铁","messageBatches":"2","status":"驳回"}]}';

    var columnList = ["customerId", "messageBatches", "customerName", "responsiblePerson", "customerType", "customerGrade", "contact",
        "phoneNumber", "idNumber", "source", "industry", "recordTime", "status"];
    //$.post(url, postData, function (result) {
    cleanTableAndPage("sendMessageTable");
    var jsonData = $.parseJSON(result);
    //画table
    $("#sendMessageTable").MakeTable(false, sm_columnData, jsonData.list, tableClass, columnList, sm_operateHtml);
    //画分页
    $("#sendMessageTable").after(comPagination(passArguments, pageSize, jsonData.totalNumber, "gotoPage", "sendMessageP"));

    //Table中操作栏 修改 解锁 查看 //TODO change
    $("#sendMessageTable button").click(function () {
        var that = $(this);
        that.val() === "edit" ?
            editMessage(that) :
            "";
    });
    //});
}

var editMessage = function (that) {
    getMessageInfo(getMessageBatchId(that));
}

//查询按钮行为 TODO Change
//$('#searchCRecordBtn').click(function () {
//    gotoPage(1);
//});

//获取id 方便提交 TODO Change
//var getContectRecordId = function (that) {
//    return that.closest("tr").find("td div")[1].innerHTML;
//}

var getMessageBatchId = function (that) {
    return that.closest("tr").find("td div")[2].innerHTML;
}

//编辑短信内容
$("#editMessage").click(function () {
    getMessageInfo($("#getMessageBatch").val());
});
var getMessageInfo = function (messageBatch) {
    var url = "";
    var postData = {"messageBatch": messageBatch};
    postAjax(url, postData, getMessageSuccess);

}
//编辑短信内容 获取内容成功 回调函数
var getMessageSuccess = function (result) {
    //TODO init Page  给短信批次 和 短信内容赋值
//            var jsonData = $.parseJSON(result);
    $("#editMessageModal").modal("show");
}

//提交审核
$("#postCheck").click(function () {
    var messageBatch = $("#getMessageBatch").val();
    showConfirm("你确定要将第" + messageBatch + "批次的短信内容提交审核吗？", function () {
        var url = "";
        var postData = {"messageBatch": messageBatch, "checkMan": $("#checkMan").val()};
        postAjax(url, postData, submitCheckSuccess);

    });
});

var submitCheckSuccess = function () {
};

//保存修改
$("#saveMessage").click(function () {
    var message = "";
    var messageBatchId = "";
    saveMessage(message, messageBatchId);
});

var saveMessage = function (message, messageBatchId) {
    var url = "";
    var postData = {"messageBatch": messageBatchId, "message": message};
    postAjax(url, postData, saveMessageSuccess);
}

var saveMessageSuccess = function () {
    $("#editMessageModal").modal("hide");
}



